<?php
/**
 * Created by IntelliJ IDEA.
 * User: ahwwl
 * Date: 18-1-4
 * Time: 下午5:40
 */

namespace app\controller;


use app\common\weixin;
use app\model\AgentList;
use app\model\CardList;
use app\model\CardLog;
use app\model\CardRefundList;
use app\model\OpinionList;
use app\model\OrderList;
use app\model\TransfersList;
use app\model\UserCardList;
use app\model\UserList;
use app\model\WalletLog;
use think\Db;
use think\Exception;
use think\Request;

/**
 * @property UserList UserList
 * @property UserCardList UserCardList
 * @property CardList CardList
 * @property TransfersList TransfersList
 * @property OpinionList OpinionList
 * @property CardLog CardLog
 * @property CardRefundList CardRefundList
 * @property AgentList AgentList
 * @property WalletLog WalletLog
 * @property OrderList OrderList
 */
class Users extends CommonController
{
    function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->UserList = new UserList();
        $this->UserCardList = new UserCardList();
        $this->CardList = new CardList();
        $this->TransfersList = new TransfersList();
        $this->OpinionList = new OpinionList();
        $this->CardLog = new CardLog();
        $this->CardRefundList = new CardRefundList();
        $this->AgentList = new AgentList();
        $this->WalletLog = new WalletLog();
        $this->OrderList = new OrderList();
    }
    function user_list()
    {
        $user_ids = $this->UserList->field("id,real_name,mobile")->select();
        $this->assign("user_ids",$user_ids);
        $this->assign("page_title","用户列表");
        return $this->fetch();
    }
    function ajax_r_user_list()
    {
        try
        {
            $user_ids = $this->UserList->field("id,real_name,mobile")->select();

            $this->resultdata["data"] = $user_ids;
            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_user_list()
    {
        try
        {
            $page = input("page",1,"intval");
            $rows = input("rows",30,"intval");
            $mobile = input("mobile","","trim");
            $real_name = input("real_name","","trim");

            $where = [];
            if(!empty($mobile))
                $where["a.mobile"] = $mobile;
            if(!empty($real_name))
                $where["a.real_name"] = ["like",$real_name."%"];

            $count = $this->UserList->alias("a")
                ->where($where)
                ->count();
            $page_total = ceil($count/$rows);
            $page = $page>$page_total?$page_total:$page;
            $page = $page < 1? 1:$page;
            $order_log = $this->UserList->alias("a")
                ->join("f_user_list b","a.r_id=b.id","left")
                ->field("a.*,b.real_name as r_id_name,b.mobile as r_id_mobile")
                ->where($where)
                ->order("a.id desc")
                ->limit(($page-1)*$rows,$rows)
                ->select();


            $this->resultdata["data"] = $order_log;
            $this->resultdata["error"] = 0;
            $this->resultdata["total"] = $count;
            $this->resultdata["page"] = $page;
            $this->resultdata["rows"] = $rows;
        }catch (\Exception $e)
        {

            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_user_info()
    {
        try
        {
            $id = input("id",0,"intval");
            if(empty($id))
                $this->E("参数不能为空");
            $info = $this->UserList->where(["id"=>$id])->find();

            $this->resultdata["data"] = $info;
            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_user_info_save()
    {
        try
        {
            $id = input("id",0,"intval");
            $mobile = input("mobile","","trim");
            $real_name = input("real_name","","trim");
            $address = input("address","","trim");
            $open_id = input("open_id","","trim");
            $r_id = input("r_id",0,"intval");

            $status = input("status",0,"intval");
            if(!in_array($status,[0,1,2])||empty($mobile)||empty($real_name)||empty($address))
                $this->E("参数不正确");

            $info = [
                "mobile"=>$mobile,
                "real_name"=>$real_name,
                "address"=>$address,
                "open_id"=>$open_id,
                "r_id"=>$r_id,
                "status"=>$status
            ];
            if($id)
            {
                $query = $this->UserList->where(["id"=>$id])->update($info);
                if(empty($query))
                {
                    $this->E("更新失败");
                }
            }else{
                $id = $this->UserList->insertGetId($info);
                if(empty($id))
                {
                    $this->E("添加失败");
                }
            }
            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }

    function user_card_list()
    {
        $user_ids = $this->UserList->field("id,real_name,mobile")->select();
        $idle_card_list = $this->CardList->where(["status"=>0])->select();
        $user_card_idle_list = $this->UserCardList->where(["user_id"=>0,"card_type"=>["GT",0]])->select();
        $this->assign("user_card_idle_list",$user_card_idle_list);
        $this->assign("idle_card_list",$idle_card_list);
        $this->assign("user_ids",$user_ids);
        $this->assign("page_title","用户卡列表");
        return $this->fetch();
    }

    function ajax_user_card_list()
    {
        try
        {
            $page = input("page",1,"intval");
            $rows = input("rows",30,"intval");
            $mobile = input("mobile","","trim");
            $real_name = input("real_name","","trim");
            $user_id = input("user_id",0,"intval");
            $card_no = input("card_no","","trim");

            $where = [];
            if(!empty($mobile))
                $where["b.mobile"] = $mobile;
            if(!empty($real_name))
                $where["b.real_name"] = ["like",$real_name."%"];
            if(!empty($user_id))
                $where["a.user_id"] =$user_id;
            if(!empty($card_no))
                $where["a.card_no"] = ["like","%".$card_no."%"];



            $count = $this->UserCardList->alias("a")
                ->join("f_user_list b","a.user_id=b.id","left")
                ->where($where)
                ->count();
            $page_total = ceil($count/$rows);
            $page = $page>$page_total?$page_total:$page;
            $page = $page < 1? 1:$page;
            $order_log = $this->UserCardList->alias("a")
                ->join("f_user_list b","a.user_id=b.id","left")
                ->join("f_agent_list al" ,"al.id=a.agent_id","left")
                ->field("a.*,b.real_name,b.mobile,al.agent_name")
                ->where($where)
                ->order("a.id desc")
                ->limit(($page-1)*$rows,$rows)
                ->select();
            $user_card_idle_list = $this->UserCardList->where(["user_id"=>0,"card_type"=>["GT",0]])->select();
            $this->resultdata["user_card_idle_list"] = $user_card_idle_list;
            $this->resultdata["data"] = $order_log;
            $this->resultdata["error"] = 0;
            $this->resultdata["total"] = $count;
            $this->resultdata["page"] = $page;
            $this->resultdata["rows"] = $rows;
        }catch (\Exception $e)
        {

            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_user_card_bind_card()
    {
        try
        {
            $user_id = input("user_id",0,"intval");
            $card_no = input("card_no","","trim");

            if(empty($user_id)||empty($card_no))
                $this->E("参数不正确");

            $info = [
                "user_id"=>$user_id,
                "card_no"=>$card_no,
                "status"=>1,
                "ctime" =>NOW_TIME
            ];
            $CardInfo = $this->CardList->where(["card_no"=>$card_no])->find();
            if(!empty($CardInfo))
            {
                $this->E("此卡为副卡不能绑定");
            }
            $card_type = $CardInfo["card_type"];
            $info["card_type"] = $card_type;
            $user_card_info =$this->UserCardList->where(["card_no"=>$card_no])->find();
            if(!empty($user_card_info))
            {
                if($user_card_info["user_id"] == $user_id)
                {
                    $this->E("此卡已经绑定");
                }else{
                    $this->E("此卡已经被别的用户绑定，解绑后可绑定");
                }

            }
            $agent_id = 0;
            $company_id = 0;
            Db::startTrans();
            try{
                $id = $this->UserCardList->insertGetId($info);
                if(empty($id))
                {
                    exception("添加失败");
                }
                $query = $this->CardLog->addInfo($card_no,$user_id,$agent_id,$company_id,"绑定");
                if(empty($query) )
                    exception("绑定记录失败");
            }catch (\Exception $e)
            {
                Db::rollback();
                exception($e->getMessage());
            }

            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }

    function ajax_user_card_bind_new_card()
    {
        try
        {
            $user_id = input("user_id",0,"intval");
            $card_no = input("card_no","","trim");

            if(empty($user_id)||empty($card_no))
                $this->E("参数不正确");


            $user_card_info = $this->UserCardList->where(["card_no"=>$card_no])->find();
            if(empty($user_card_info))
            {
                $this->E("此卡没有购买，不能绑定");
            }
            if($user_card_info["card_type"] == 0)
            {
                $this->E("此卡为老卡，不能绑定");
            }
            if($user_card_info["user_id"]>0)
            {
                if($user_card_info["user_id"] == $user_id)
                {
                    $this->E("此卡已经绑定");
                }else{
                    $this->E("此卡已经被别的用户绑定，解绑后可绑定");
                }
            }
            $agent_id = $user_card_info["agent_id"];
            $company_id = $user_card_info["company_id"];
            Db::startTrans();
            try{
                $id = $this->UserCardList->where(["user_id"=>0,"card_no"=>$card_no])->update(["user_id"=>$user_id]);
                if(empty($id))
                {
                    exception("绑定失败");
                }
                $query = $this->CardLog->addInfo($card_no,$user_id,$agent_id,$company_id,"绑定");
                if(empty($query) )
                    exception("绑定记录失败");
            }catch (\Exception $e)
            {
                Db::rollback();
                exception($e->getMessage());
            }

            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_user_card_unbind_card()
    {
        try
        {
            $id = input("id",0,"intval");
            if(empty($id))
                $this->E("参数不能为空");
            $info = $this->UserCardList->where(["id"=>$id])->find();
            if(empty($info))
            {
                $this->E("此卡不存在");
            }
            $card_no = $info["card_no"];
            $user_id = $info["user_id"];
            $agent_id = $info["agent_id"];
            $company_id = $info["company_id"];
            Db::startTrans();
            try{
                if($info["card_type"] == 0)
                {
                    $query = $this->UserCardList->where(["id"=>$id])->delete();
                }else{
                    $query = $this->UserCardList->where(["id"=>$id])->update(["user_id"=>0]);
                }

                if(empty($query))
                {
                    exception("解绑失败");
                }
                $query = $this->CardLog->addInfo($card_no,$user_id,$agent_id,$company_id,"解绑");
                if(empty($query) )
                    exception("解绑记录失败");
                Db::commit();
            }catch (\Exception $e)
            {
                Db::rollback();
                exception($e->getMessage());
            }

            $this->resultdata["data"] = $info;
            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_user_card_info()
    {
        try
        {
            $id = input("id",0,"intval");
            if(empty($id))
                $this->E("参数不能为空");
            $info = $this->UserCardList->where(["id"=>$id])->find();

            $this->resultdata["data"] = $info;
            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }

    function ajax_user_card_info_save()
    {
        try
        {
            $id = input("id",0,"intval");
            $user_id = input("user_id",0,"intval");
            $card_no = input("card_no","","trim");
            $status = input("status",0,"intval");
            $card_type = input("card_type",0,"intval");
            if(!in_array($status,[0,1,2])||empty($user_id)||empty($card_no))
                $this->E("参数不正确");

            $info = [
                "user_id"=>$user_id,
                "card_no"=>$card_no,
                "card_type"=>$card_type,
                "status"=>$status,
                "ctime" =>NOW_TIME
            ];
            $CardInfo = $this->CardList->where(["card_no"=>$card_no])->find();
            if(!empty($CardInfo) && $CardInfo["card_type"] != $card_type)
                $this->E("卡类型错误");
            $UserCardListInfo = [];
            if($id>0)
            {
                $UserCardListInfo = $this->UserCardList->where(["id"=>$id])->find();

                if(empty($UserCardListInfo))
                {
                    $this->E("信息不存在");
                }
                if(!empty($UserCardListInfo["card_no"])&&$UserCardListInfo["card_no"] != $card_no&&$UserCardListInfo["card_type"] !=0)
                {
                    $query = $this->CardList->where(["card_no"=>$UserCardListInfo["card_no"],"status"=>1])->update(["status"=>0]);
                    if(!$query)
                        $this->E("原副卡更新状态失败");
                }
            }

            if($card_type != 0)
            {

                if(!empty($CardInfo))
                {
                    if(empty($UserCardListInfo) ||(!empty($UserCardListInfo)&&$UserCardListInfo["card_no"] != $card_no))
                    {

                        $query = $this->CardList->where(["card_no"=>$card_no,"status"=>0])->update(["status"=>1]);
                        if(!$query)
                            $this->E("副卡状态使用中或已禁用");
                    }
                }
            }
            if($id)
            {
                $query = $this->UserCardList->where(["id"=>$id])->update($info);
                if(empty($query))
                {
                    $this->E("更新失败");
                }
            }else{
                $id = $this->UserCardList->insertGetId($info);
                if(empty($id))
                {
                    $this->E("添加失败");
                }
            }
            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_card_refund()
    {
        try
        {
            $mode = input("mode",0,"intval");
            $card_no = input("card_no","","trim");
            $money = input("money",0,"intval");
            $refund_rebate = input("refund_rebate",0,"intval");
            if(!in_array($mode,[0,1])||empty($card_no))
                $this->E("参数不能为空");
            $info = $this->UserCardList->where(["card_no"=>$card_no])->find();
            if(empty($info))
                exception("卡号信息不存在");
            $agent_id = $info["agent_id"];
            $user_id = $info["user_id"];
            $company_id = $info["company_id"];
            $money = $money*10000;
            $refund_rebate = $refund_rebate*10000;
            $refund_id = 0;
            if($mode == 0)
            {
                if(empty($agent_id))
                    exception("卡不属于代理");
                $refund_id = $agent_id;
            }

            if($mode == 1 )
            {
                if(empty($user_id))
                    exception("卡号没有绑定用户");
                if($refund_rebate>0)
                    exception("用户退卡无法退还代理佣金");
                $refund_id = $user_id;
            }
            $card_refund_info = [
                "card_no"=>$card_no,
                "money"=>$money,
                "mode"=>$mode,
                "refund_id" =>$refund_id,
                "status" => 0,
                "ctime" =>NOW_TIME,
                "mtime" =>NOW_TIME,
            ];
            Db::startTrans();
            try{
                $query = $this->CardList->where(["card_no"=>$card_no,"status"=>1])->update(["status"=>0]);
                if(empty($query))
                    exception("更新卡库状态失败");
                $query = $this->UserCardList->where(["card_no"=>$card_no])->delete();
                if(empty($query))
                    exception("退卡失败");
                $query = $this->CardRefundList->insert($card_refund_info);
                if(empty($query))
                    exception("插入退卡信息失败");
                $query = $this->CardLog->addInfo($card_no,$user_id,$agent_id,$company_id,"退卡");
                if(empty($query))
                    exception("插入退卡记录失败");
                if($refund_rebate >0)
                {
                    $info = ["agent_id" => $agent_id,
                        "from_user_id"=>$user_id,
                        "order_no" => "",
                        "type"=>1,
                        "money" => -$refund_rebate,
                        "level"=>0,
                        "info" => "退卡时退还佣金",
                        "ctime" => NOW_TIME];
                    $query = $this->WalletLog->insert($info);
                    if(empty($query)) exception("退还代理返佣记录插入失败");
                    $query = $this->AgentList->where(["id" => $agent_id])
                        ->dec("rebate",$refund_rebate)
                        ->dec("total_rebate",$refund_rebate)
                        ->update();
                    if(empty($query)) exception("更新代理返佣金额失败");
                }
                Db::commit();
            }catch (\Exception $e)
            {
                Db::rollback();
                \exception($e->getMessage());
            }



            $this->resultdata["data"] = $info;
            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_card_change()
    {
        try
        {
            $old_card_no = input("old_card_no",0,"intval");
            $card_no = input("card_no","","trim");
            $freight = input("freight",0,"intval");
            $card_fee = input("card_fee",0,"intval");
            if(empty($old_card_no)||empty($card_no))
                $this->E("参数不能为空");
            $info = $this->UserCardList->where(["card_no"=>$old_card_no])->find();
            if(empty($info))
                exception("老卡号信息不存在");
            $freight = $freight*10000;
            $card_fee = $card_fee*10000;
            $agent_id = $info["agent_id"];
            $user_id = $info["user_id"];
            $company_id = $info["company_id"];
            $real_name ="";
            $address = "";
            $mobile = "";
            $user_info= $this->UserList->where(["id"=>$user_id])->find();
            if(!empty($user_info))
            {
                $real_name = $user_info["real_name"];
                $address = $user_info["address"];
                $mobile = $user_info["mobile"];

            }
            $info = $this->CardList->where(["card_no"=>$card_no,"status"=>0])->find();
            if(empty($info))
                exception("卡号信息不存在");
            $card_type = $info["card_type"];
            $order_no = get_order_no("D");
            $total_money = $freight+$card_fee;
            $info = [
                "order_no" => $order_no,
                "user_id" => $user_id,
                "agent_id" => $agent_id,
                "company_id" => $company_id,
                "card_no" =>$card_no,
                "card_type" =>$card_type,
                "basic_price"=>0,
                "cost" => 0,
                "money" => 0,
                "freight" =>$freight,
                "card_fee" => $card_fee,
                "discount_money"=>0,
                "total_money" =>$total_money,
                "pay_status"=>1,
                "delivery_status"=>1,
                "mode" =>1,
                "real_name"=>$real_name,
                "address" =>$address,
                "mobile" => $mobile,
                "ctime" => NOW_TIME,
                "mtime" => NOW_TIME,
                "ip" => $this->request->ip()
            ];
            Db::startTrans();
            try{
                $query = $this->CardList->where(["card_no" =>$card_no,"card_type"=>$card_type,"status"=>0])->update(["status"=>1]);
                if(empty($query))
                    $this->E("卡号不存在或已经使用");
                $query = $this->CardList->where(["card_no" =>$old_card_no,"card_type"=>$card_type,"status"=>1])->update(["status"=>0]);
                if(empty($query))
                    $this->E("卡号不存在或已经使用");

                $query = $this->OrderList->insert($info);
                if(empty($query))
                    $this->E("添加订单失败");
                $query = $this->UserCardList->insert(["user_id"=>$user_id,"agent_id"=>$agent_id,"card_no"=>$card_no,"card_type"=>$card_type,"card_fee" => $card_fee,"status"=>1, "ctime" =>NOW_TIME]);
                if(empty($query))
                    $this->E("用户绑定卡片失败");
                $query = $this->UserCardList->where(["card_no"=>$old_card_no])->delete();
                if(empty($query))
                    $this->E("老卡删除失败");
                $this->CardLog->addInfo($old_card_no,$user_id,$agent_id,$company_id,$old_card_no."换新卡:".$card_no);
                $this->CardLog->addInfo($card_no,$user_id,$agent_id,$company_id,$old_card_no."换新卡:".$card_no);

                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $this->E($e->getMessage());
            }
            $this->order_log($order_no,"添加新卡订单，卡号：".$card_no.",老卡:".$old_card_no);
            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }

    function transfers_list()
    {
        $user_ids = $this->UserList->field("id,real_name,mobile")->select();
        $user_card_idle_list = $this->UserCardList->where(["user_id"=>0,"card_type"=>["GT",0]])->select();
        $this->assign("user_card_idle_list",$user_card_idle_list);
        $this->assign("user_ids",$user_ids);
        $this->assign("page_title","用户卡列表");
        return $this->fetch();
    }
    function ajax_transfers_list()
    {
        try
        {
            $page = input("page",1,"intval");
            $rows = input("rows",30,"intval");
            $mobile = input("mobile","","trim");
            $real_name = input("real_name","","trim");
            $user_id = input("user_id",0,"intval");

            $where = [];
            if(!empty($mobile))
                $where["b.mobile"] = $mobile;
            if(!empty($real_name))
                $where["b.real_name"] = ["like",$real_name."%"];
            if(!empty($user_id))
                $where["a.user_id"] =$user_id;



            $count = $this->TransfersList->alias("a")
                ->join("f_user_list b","a.user_id=b.id","left")
                ->where($where)
                ->count();
            $page_total = ceil($count/$rows);
            $page = $page>$page_total?$page_total:$page;
            $page = $page < 1? 1:$page;
            $order_log = $this->TransfersList->alias("a")
                ->join("f_user_list b","a.user_id=b.id","left")
                ->field("a.*,b.real_name,b.mobile")
                ->where($where)
                ->order("a.id desc")
                ->limit(($page-1)*$rows,$rows)
                ->select();
            $this->resultdata["data"] = $order_log;
            $this->resultdata["error"] = 0;
            $this->resultdata["total"] = $count;
            $this->resultdata["page"] = $page;
            $this->resultdata["rows"] = $rows;
        }catch (\Exception $e)
        {

            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_transfers_order()
    {
        try
        {
            $id = input("id",0,"intval");
            if(empty($id))
                $this->E("参数不能为空");
            $info = $this->TransfersList
                ->where(["id"=>$id])->field("open_id,open_type,trade_no,money")->find();
            if(empty($info))
                $this->E("订单不存在");
            $trade_no = $info["trade_no"];
            $money = $info["money"];
            $openId = $info["open_id"];
            $open_type = $info["open_type"];
            $weixin = new weixin();
            $server_ip = $this->request->server("SERVER_ADDR");
            $weixin_result = $weixin->mmpaymkttransfers($trade_no,$openId,intval($money/100),"提现",$server_ip,$open_type);

            if(!empty($weixin_result) &&$weixin_result["error"] == 0)
            {
                $trade_no1 = $weixin_result["partner_trade_no"];
                $payment_no = $weixin_result["payment_no"];
                $payment_time = $weixin_result["payment_time"];
                if($trade_no1 == $trade_no)
                {
                    $this->TransfersList->where(["trade_no"=>$trade_no])->update(["payment_no"=>$payment_no,"payment_time"=>$payment_time,"status"=>1]);
                    $this->resultdata["msg"] = "提现成功";
                }
            }else{
                if($weixin_result["error"])
                {
                    \exception($weixin_result["msg"]);
                }
                $this->resultdata["msg"] = "审核中";
            }

            $this->resultdata["data"] = $info;
            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_transfers_info()
    {
        try
        {
            $id = input("id",0,"intval");
            if(empty($id))
                $this->E("参数不能为空");
            $info = $this->TransfersList->where(["id"=>$id])->find();

            $this->resultdata["data"] = $info;
            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_transfers_info_save()
    {
        try
        {
            $user_id = input("user_id",0,"intval");
            $money = input("money",0,"intval");
            if(empty($user_id)||empty($money))
                $this->E("参数不能为空");
            $money = $money*10000;
            $user_info = $this->UserList->where(["id"=>$user_id])->find();
            if(empty($user_info))
                $this->E("用户不存在");
            $open_id = $user_info["open_id"];
            if(empty($open_id))
                $this->E("没有绑定微信");
            $trade_no = get_order_no("T");
            $info = [
                "trade_no" =>$trade_no,
                "user_id"=>$user_id,
                "open_id"=>$open_id,
                "money"=>$money,
                "ctime"=>NOW_TIME,
                "ip"=>$this->request->ip()
            ];
            Db::startTrans();
            try{
                $query = $this->UserList->where(["id"=>$user_id,"rebate"=>[">=",$money]])->setDec("rebate",$money);
                if(empty($query)) exception("更新佣金失败");
                $query = $this->TransfersList->insert($info);
                if(empty($query))
                    $this->E("添加失败");
                Db::commit();
            }catch (Exception $e)
            {
                // 回滚事务
                Db::rollback();
                $this->E($e->getMessage());
            }

            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_transfers_set_fail()
    {
        try
        {
            $id = input("id",0,"intval");
            if(empty($id))
                $this->E("参数不能为空");
            $info = $this->TransfersList->where(["id"=>$id,"status"=>0])->find();
            if(empty($info))
                $this->E("订单不存在或状态不在审核中");
            $user_id =$info["user_id"];
            $money =$info["money"];
            Db::startTrans();
            try{
                $query = $this->UserList->where(["id"=>$user_id])->setInc("rebate",$money);
                if(empty($query)) exception("更新佣金失败");
                $query = $this->TransfersList->where(["id"=>$id,"status"=>0])->update(["status"=>2]);
                if(empty($query))
                    $this->E("更新状态失败");
                Db::commit();
            }catch (Exception $e)
            {
                // 回滚事务
                Db::rollback();
                $this->E($e->getMessage());
            }

            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }

    function opinion_list()
    {
        $this->assign("page_title","用户留言列表");
        return $this->fetch();
    }
    function ajax_opinion_list()
    {
        try
        {
            $page = input("page",1,"intval");
            $rows = input("rows",30,"intval");
            $mobile = input("mobile","","trim");
            $real_name = input("real_name","","trim");
            $user_id = input("user_id",0,"intval");

            $where = [];
            if(!empty($mobile))
                $where["b.mobile"] = $mobile;
            if(!empty($real_name))
                $where["b.real_name"] = ["like",$real_name."%"];
            if(!empty($user_id))
                $where["a.user_id"] =$user_id;



            $count = $this->OpinionList->alias("a")
                ->join("f_user_list b","a.user_id=b.id","left")
                ->where($where)
                ->count();
            $page_total = ceil($count/$rows);
            $page = $page>$page_total?$page_total:$page;
            $page = $page < 1? 1:$page;
            $order_log = $this->OpinionList->alias("a")
                ->join("f_user_list b","a.user_id=b.id","left")
                ->join("f_admin c","c.id=a.admin_id","left")
                ->field("a.*,b.real_name,b.mobile,c.user_name as admin_name")
                ->where($where)
                ->order("a.id desc")
                ->limit(($page-1)*$rows,$rows)
                ->select();
            $this->resultdata["data"] = $order_log;
            $this->resultdata["error"] = 0;
            $this->resultdata["total"] = $count;
            $this->resultdata["page"] = $page;
            $this->resultdata["rows"] = $rows;
        }catch (\Exception $e)
        {

            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_set_opinion_status()
    {
        try
        {
            $id = input("id",0,"intval");
            $status = input("status",0,"intval");
            if(empty($id) ||!in_array($status,[0,1]))
                $this->E("参数不能为空");
            $query = $this->OpinionList->where(["id"=>$id,"status"=>0])->update(["status"=>$status,"admin_id"=>$this->admin_id,"mtime"=>NOW_TIME]);
            if(empty($query))
                $this->E("更新数据失败，请联系技术");

            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }

    function card_refund_list()
    {
        $this->assign("page_title","用户卡退款列表");
        return $this->fetch();
    }
    function ajax_card_refund_list()
    {
        try
        {
            $page = input("page",1,"intval");
            $rows = input("rows",30,"intval");
            $mobile = input("mobile","","trim");
            $real_name = input("real_name","","trim");
            $user_id = input("user_id",0,"intval");

            $where = [];
            if(!empty($mobile))
                $where["b.mobile"] = $mobile;
            if(!empty($real_name))
                $where["b.real_name"] = ["like",$real_name."%"];
            if(!empty($user_id))
                $where["a.user_id"] =$user_id;



            $count = $this->CardRefundList->alias("a")
                ->join("f_user_list b","a.refund_id=b.id","left")
                ->join("f_agent_list c","a.refund_id=c.id","left")
                ->where($where)
                ->count();
            $page_total = ceil($count/$rows);
            $page = $page>$page_total?$page_total:$page;
            $page = $page < 1? 1:$page;
            $order_log = $this->CardRefundList->alias("a")
                ->join("f_user_list b","a.refund_id=b.id","left")
                ->join("f_agent_list c","a.refund_id=c.id","left")
                ->join("f_admin d","d.id=a.admin_id","left")
                ->field("a.*,b.real_name,b.mobile,c.agent_name,d.user_name as admin_name")
                ->where($where)
                ->order("a.id desc")
                ->limit(($page-1)*$rows,$rows)
                ->select();
            $this->resultdata["data"] = $order_log;
            $this->resultdata["error"] = 0;
            $this->resultdata["total"] = $count;
            $this->resultdata["page"] = $page;
            $this->resultdata["rows"] = $rows;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_set_card_refund()
    {
        try
        {
            $id = input("id",0,"intval");
            if(empty($id) )
                $this->E("参数不能为空");
            $info = $this->CardRefundList->where(["id"=>$id])->find();
            if(empty($info))
                $this->E("记录不存在");
            if($info["status"] != 0)
                $this->E("记录已经处理");
            $mode = $info["mode"];
            $money = $info["money"];
            $refund_id = $info["refund_id"];
            $open_id = $info["open_id"];
            $open_type = $info["open_type"];
            $query = $this->CardRefundList->where(["id"=>$id,"status"=>0])->update(["status"=>1,"admin_id"=>$this->admin_id,"mtime"=>NOW_TIME]);
            if(empty($query))
                $this->E("更新处理中状态失败，请联系技术");
            try{
                $refund_type = 0;
                $pay_order_no= "";
                if($mode == 0)
                {
                    Db::startTrans();
                    try{
                        $info = ["agent_id" => $refund_id,
                            "from_user_id"=>0,
                            "order_no" => "",
                            "type"=>2,
                            "money" => $money,
                            "level"=>0,
                            "info" => "退卡时退款",
                            "ctime" => NOW_TIME];
                        $query = $this->WalletLog->insert($info);
                        if(empty($query)) exception("退还代理退款记录插入失败");

                        $query = $this->AgentList->where(["id" => $refund_id])->setInc("balance",$money);
                        if(empty($query)) exception("更新代理退款金额失败");
                        Db::commit();
                        $refund_type = 3;
                    }catch (\Exception $e)
                    {
                        Db::rollback();
                        \exception($e->getMessage());
                    }
                }elseif($mode == 1){

                    if(empty($open_id)||!in_array($open_type,[1,2]))
                    {
                        \exception("客户没有确认微信账号");
                    }
                    $refund_type = $open_type;
                    $weixin = new weixin();
                    $pay_order_no = get_order_no("T");
                    $server_ip = $this->request->server("SERVER_ADDR");
                    $result = $weixin->mmpaymkttransfers($pay_order_no,$open_id,intval($money/100),"退款",$server_ip,$open_type);
                    if(empty($result) ||$result["error"]==1)
                    {
                        \exception("退款失败");
                    }
                }else{
                    \exception("类型错误");
                }
                $query = $this->CardRefundList->where(["id"=>$id,"status"=>1])->update(["pay_order_no"=>$pay_order_no,"refund_type"=>$refund_type,"status"=>2,"admin_id"=>$this->admin_id,"mtime"=>NOW_TIME]);
                if(empty($query))
                    $this->E("更新处理完成状态失败，请联系技术");
            }catch (\Exception $e)
            {
                $this->CardRefundList->where(["id"=>$id,"status"=>1])->update(["status"=>0]);
                \exception($e->getMessage());
            }



            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
}